package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type UrlController struct{}

func (that UrlController) UrlConfig(ctx *fasthttp.RequestCtx) {
	//admin := session.AdminGet(ctx)
	//if admin["loginuser"] != "admin" {
	//	helper.RetData(ctx, false, "权限不足")
	//	return
	//}
	param := model.UrlParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetUrlConfig(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that UrlController) UpdateUrlConfig(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.UrlConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("域名列表配置更新", ctx)
	err = model.UpdateUrlConfig(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that UrlController) DeleteUrlConfig(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.UrlConfig{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("域名列表配置删除", ctx)
	err = model.UpdateUrlConfig(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
